package com.hjqjl.whdemo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.databinding.DialogBottomBinding;

public class DialogBottom extends Dialog {
    private Context context;
    private final OnClickMyListener listener;

    public static DialogBottom show(Context context, OnClickMyListener listener) {
        DialogBottom dialogTop = new DialogBottom(context, listener);
        dialogTop.show();
        return dialogTop;
    }

    public DialogBottom(@NonNull Context context, OnClickMyListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉dialog的标题，需要在setContentView()之前
        DialogBottomBinding binding = DialogBottomBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Window window = getWindow();
        if (window != null) {
            //去掉dialog默认的padding
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            //设置dialog的位置在底部
            lp.gravity = Gravity.BOTTOM;
            //设置dialog的动画
            lp.windowAnimations = R.style.BottomDialogAnimation;
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable());
        }

        binding.tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onMyClick(DialogBottom.this);
                }
            }
        });
        binding.tvCancel.setOnClickListener(v -> dismiss());
    }

    public interface OnClickMyListener {
        void onMyClick(Dialog dialog);
    }
}
